package org.example.everyday;

import org.omg.CORBA.INTERNAL;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * <p>
 *
 * @author anlu
 * @date 2022/7/14 8:49
 */
public class WordFilter {
    Map<String,Integer> dictionary;

    public WordFilter(String[] words) {
        dictionary = new HashMap<String, Integer>();
        for (int i = 0; i < words.length; i++){
            String word = words[i];
            int m = word.length();
            for(int prefixLength = 1; prefixLength <= m; prefixLength++){
                for(int suffixLength = 1; suffixLength <= m; suffixLength++){
                    dictionary.put(word.substring(0,prefixLength) + "#" + word.substring(m - suffixLength),i);
                }
            }
        }
    }

    public int f(String pref, String suff) {
        return dictionary.getOrDefault(pref + "#" + suff, - 1);
    }

    public static void main(String[] args) {
        String[] words = new String[]{"apple"};
        WordFilter wordFilter = new WordFilter(words);
        int res = wordFilter.f("ap", "e"); // 返回 0 ，因为下标为 0 的单词：前缀 prefix = "a" 且 后缀 suff = "e" 。
        System.out.println(res);

    }
}
